package com.xindaoapp.henghua.application;

import android.app.Application;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;

import com.library.utils.PreferenceFile;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xindaoapp.henghua.R;
import com.xindaoapp.henghua.db.IDBHelper;
import com.xindaoapp.henghua.db.XinDaoDBHelper;
import com.xindaoapp.henghua.entry.UserInfo;
import com.xindaoapp.henghua.protocol.MoccaApi;
import com.xindaoapp.henghua.protocol.MoccaApiImpl;
import com.xindaoapp.henghua.utils.UpdateDataChanged;

public class XinDaoApplication extends Application {
	private static XinDaoApplication mIns;
	private MoccaApi mMoccaApi;
	private UserInfo userInfo;
	private IDBHelper mXinDaoDBHelper;
	private UpdateDataChanged mUpdateDataChanged;

	public static String versionName;
	public static String packageName;

	public final static Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
		}

	};

	@Override
	public void onCreate() {
		super.onCreate();
		mIns = this;
		PreferenceFile.init(mIns);
		mMoccaApi = new MoccaApiImpl();
		mXinDaoDBHelper = new XinDaoDBHelper(mIns);
		mUpdateDataChanged = new UpdateDataChanged();

		try {
			packageName = getPackageName();
			versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().resetViewBeforeLoading(true)
				.imageScaleType(ImageScaleType.EXACTLY).showImageForEmptyUri(R.drawable.empty_photo)
				.bitmapConfig(Bitmap.Config.RGB_565).showImageOnFail(R.drawable.empty_photo)
				.considerExifParams(true)
//		.displayer(new FadeInBitmapDisplayer(300))
				.cacheInMemory(true).cacheOnDisc(true).build();

		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
				.defaultDisplayImageOptions(defaultOptions).discCacheSize(50 * 1024 * 1024)//
				.discCacheFileCount(100)// 缓存一百张图片
				.writeDebugLogs().build();
		ImageLoader.getInstance().init(config);

	}

	public static XinDaoApplication get() {
		return mIns;
	}

	public static Handler getHandler() {
		return mHandler;
	}

	public UpdateDataChanged getUpdateDataChanged() {
		return mUpdateDataChanged;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public MoccaApi getMoccaApi() {
		return mMoccaApi;
	}

	public IDBHelper getDBHelper() {
		return mXinDaoDBHelper;
	}

}
